package cn.czg.cms.netty;

import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class NettyChannelManager {
    private final ConcurrentHashMap<Long, Channel> userChannelMap = new ConcurrentHashMap<>();

    public void add(Long userId, Channel channel) {
        userChannelMap.put(userId, channel);
    }

    public void remove(Long userId) {
        userChannelMap.remove(userId);
    }

    public Long getUserId(Channel channel) {
        return userChannelMap.entrySet().stream()
                .filter(e -> e.getValue().equals(channel))
                .map(Map.Entry::getKey)
                .findFirst().orElse(null);
    }

    public Channel getChannel(Long userId) {
        return userChannelMap.get(userId);
    }
}



